package com.mathassistant.server;

import java.util.ArrayList;

import javax.jdo.PersistenceManager;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.mathassistant.client.service.TheoryService;
import com.mathassistant.server.domain.Theory;
import com.mathassistant.server.domain.Topic;
import com.mathassistant.shared.TheoryDTO;
import com.mathassistant.shared.TopicDTO;

@SuppressWarnings("serial")
public class TheoryServiceImpl extends RemoteServiceServlet implements TheoryService{

	@Override
	public void createTheory(TheoryDTO theory, TopicDTO topic) {
		// TODO Auto-generated method stub
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			Topic tp = pm.getObjectById(Topic.class, topic.getId());
			Theory lesson = new Theory(theory);
			pm.makePersistent(lesson);
			tp.getTheorys().add(lesson);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {
			pm.close();
		}
	}

	@Override
	public void updateTheory(TheoryDTO theory) {
		// TODO Auto-generated method stub
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			Theory lesson = pm.getObjectById(Theory.class, theory.getId());
			lesson.updateFromDTO(theory);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {
			pm.close();
		}
	}

	@Override
	public void deleteTheory(TheoryDTO theory) {
		// TODO Auto-generated method stub
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			Theory lesson = pm.getObjectById(Theory.class, theory.getId());
			if(lesson != null){
				pm.deletePersistent(pm.getObjectById(Theory.class, theory.getId()));
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {
			pm.close();
		}
	}

	@Override
	public int getCount(TopicDTO topic) {
		// TODO Auto-generated method stub
		int result = -1;
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			Topic itp = pm.getObjectById(Topic.class, topic.getId());
			if(itp != null){
				ArrayList<Theory> list = itp.getTheorys();
				if(list != null){
					result = list.size();
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {
			pm.close();
		}
		return result;
	}

	@Override
	public TheoryDTO getTheory(int index, TopicDTO topic) {
		// TODO Auto-generated method stub
		PersistenceManager pm = PMF.get().getPersistenceManager();
		TheoryDTO result = null;
		try {
			Topic itp = pm.getObjectById(Topic.class, topic.getId());
			if(itp != null){
				
				ArrayList<Theory> list = itp.getTheorys();
				if(list != null && list.size() > index){
					result = new TheoryDTO(list.get(index).toDTO());
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {
			pm.close();
		}
		return result;
	}

	@Override
	public ArrayList<TheoryDTO> getTheorys(int index, TopicDTO topic) {
		// TODO Auto-generated method stub
		PersistenceManager pm = PMF.get().getPersistenceManager();
		ArrayList<TheoryDTO> result = null;
		try {
			Topic itp = pm.getObjectById(Topic.class, topic.getId());
			if(itp != null){
				ArrayList<Theory> list = itp.getTheorys();
				if((list != null) && (list.size() > index)){
					result = new ArrayList<TheoryDTO>();
					for(int i = index; i < index+10; i++){
						if(i >= list.size())
							break;
						result.add(list.get(i).toDTO());
					}
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {
			pm.close();
		}
		return result;
	}

	@Override
	public ArrayList<TheoryDTO> getTheorys(TopicDTO topic) {
		// TODO Auto-generated method stub
		PersistenceManager pm = PMF.get().getPersistenceManager();
		ArrayList<TheoryDTO> result = null;
		try {
			Topic itp = pm.getObjectById(Topic.class, topic.getId());
			if(itp != null){
				ArrayList<Theory> list = itp.getTheorys();
				if(list != null){
					result = new ArrayList<TheoryDTO>();
					for(int i = 0; i < list.size(); i++){
						result.add(list.get(i).toDTO());
					}
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {
			pm.close();
		}
		return result;
	}
}
